home *** CD-ROM | disk | FTP | other *** search
/ Plug Into a Brand New World / Plug Into a Brand New World Version 1.3.BIN / KW-585S25 / Linux / usb-serial.h < prev    next >
C/C++ Source or Header  |  2002-02-13  |  8KB  |  232 lines

  1. /*
  2.  * USB Serial Converter driver
  3.  *
  4.  *    Copyright (C) 1999, 2000
  5.  *        Greg Kroah-Hartman (greg@kroah.com)
  6.  *
  7.  *    This program is free software; you can redistribute it and/or modify
  8.  *    it under the terms of the GNU General Public License as published by
  9.  *    the Free Software Foundation; either version 2 of the License, or
  10.  *    (at your option) any later version.
  11.  *
  12.  * See Documentation/usb/usb-serial.txt for more information on using this driver
  13.  *
  14.  * (10/05/2000) gkh
  15.  *    Added interrupt_in_endpointAddress and bulk_in_endpointAddress to help
  16.  *    fix bug with urb->dev not being set properly, now that the usb core
  17.  *    needs it.
  18.  * 
  19.  * (09/11/2000) gkh
  20.  *    Added usb_serial_debug_data function to help get rid of #DEBUG in the
  21.  *    drivers.
  22.  *
  23.  * (08/28/2000) gkh
  24.  *    Added port_lock to port structure.
  25.  *
  26.  * (08/08/2000) gkh
  27.  *    Added open_count to port structure.
  28.  *
  29.  * (07/23/2000) gkh
  30.  *    Added bulk_out_endpointAddress to port structure.
  31.  *
  32.  * (07/19/2000) gkh, pberger, and borchers
  33.  *    Modifications to allow usb-serial drivers to be modules.
  34.  *
  35.  * 
  36.  */
  37. #ifndef __LINUX_USB_SERIAL_H
  38. #define __LINUX_USB_SERIAL_H
  39.  
  40. #include <linux/config.h>
  41.  
  42. #define SERIAL_TTY_MAJOR      188     /* Nice legal number now */
  43. #define SERIAL_TTY_MINORS     255     /* loads of devices :) */
  44.  
  45. #define MAX_NUM_PORTS         8       /* The maximum number of ports one device can grab at once */
  46.  
  47. #define USB_SERIAL_MAGIC      0x6702  /* magic number for usb_serial struct */
  48. #define USB_SERIAL_PORT_MAGIC 0x7301  /* magic number for usb_serial_port struct */
  49.  
  50. /* parity check flag */
  51. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK))
  52. struct usb_serial_port {
  53.   int               magic;
  54.   struct usb_serial *serial;                      /* pointer back to the owner of this port */
  55.   struct tty_struct *tty;                         /* the coresponding tty for this port */
  56.   unsigned char     number;
  57.   char              active;                       /* someone has this device open */
  58.  
  59.   unsigned char     *interrupt_in_buffer;
  60.   struct urb        *interrupt_in_urb;
  61.   __u8              interrupt_in_endpointAddress;
  62.  
  63.   unsigned char     *bulk_in_buffer;
  64.   struct urb        *read_urb;
  65.   __u8              bulk_in_endpointAddress;
  66.  
  67.   unsigned char     *bulk_out_buffer;
  68.   int               bulk_out_size;
  69.   struct urb        *write_urb;
  70.   __u8              bulk_out_endpointAddress;
  71.  
  72.   wait_queue_head_t write_wait;
  73.  
  74.   struct tq_struct  tqueue;                       /* task queue for line discipline waking up */
  75.   int               open_count;                   /* number of times this port has been opened */
  76.   spinlock_t        port_lock;
  77.  
  78.   void *private;                                  /* data private to the specific port */
  79. };
  80.  
  81. struct usb_serial {
  82.   int                           magic;
  83.   struct usb_device             *dev;
  84.   struct usb_serial_device_type *type;            /* the type of usb serial device this is */
  85.   struct usb_interface          *interface;       /* the interface for this device */
  86.   struct tty_driver             *tty_driver;      /* the tty_driver for this device */
  87.   unsigned char                 minor;            /* the starting minor number for this device */
  88.   unsigned char                 num_ports;        /* the number of ports this device has */
  89.   char                          num_interrupt_in; /* number of interrupt in endpoints we have */
  90.   char                          num_bulk_in;      /* number of bulk in endpoints we have */
  91.   char                          num_bulk_out;     /* number of bulk out endpoints we have */
  92.   struct usb_serial_port        port[MAX_NUM_PORTS];
  93.  
  94.   void *private;  /* data private to the specific driver */
  95. };
  96.  
  97. #define MUST_HAVE_NOT 0x01
  98. #define MUST_HAVE     0x02
  99. #define DONT_CARE     0x03
  100.  
  101. #define HAS           0x02
  102. #define HAS_NOT       0x01
  103.  
  104. #define NUM_DONT_CARE (-1)
  105. /* This structure defines the individual serial converter. */
  106. struct usb_serial_device_type {
  107.   char                        *name;
  108.   const struct usb_device_id  *id_table;
  109.   char                        needs_interrupt_in;
  110.   char                        needs_bulk_in;
  111.   char                        needs_bulk_out;
  112.   char                        num_interrupt_in;
  113.   char                        num_bulk_in;
  114.   char                        num_bulk_out;
  115.   char                        num_ports;  /* number of serial ports this device has */
  116.  
  117.   struct list_head            driver_list;
  118.  
  119.   /* function call to make before accepting driver */
  120.  
  121.   /* return 0 to continue initialization, anything else to abort */
  122.   int (*startup) (struct usb_serial *serial);
  123.  
  124.   void (*shutdown) (struct usb_serial *serial);
  125.  
  126.   /* serial function calls */
  127.   int (*open) (struct usb_serial_port *port, struct file *filp);
  128.   void (*close) (struct usb_serial_port *port, struct file *filp);
  129.   int (*write) (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count);
  130.   int (*write_room) (struct usb_serial_port *port);
  131.   int (*ioctl) (struct usb_serial_port *port, struct file *file, unsigned int cmd, unsigned long arg);
  132.   void (*set_termios) (struct usb_serial_port *port, struct termios *old);
  133.   void (*break_ctl) (struct usb_serial_port *port, int break_state);
  134.   int (*chars_in_buffer) (struct usb_serial_port *port);
  135.   void (*throttle) (struct usb_serial_port *port);
  136.   void (*unthrottle) (struct usb_serial_port *port);
  137.  
  138.   void (*read_int_callback) (struct urb *urb);
  139.   void (*read_bulk_callback) (struct urb *urb);
  140.   void (*write_bulk_callback) (struct urb *urb);
  141. };
  142.  
  143. extern int  usb_serial_register(struct usb_serial_device_type *new_device);
  144. extern void usb_serial_deregister(struct usb_serial_device_type *device);
  145.  
  146. /* determine if we should include the EzUSB loader functions */
  147. #if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || \
  148.     defined(CONFIG_USB_SERIAL_WHITEHEAT) || \
  149.     defined(CONFIG_USB_SERIAL_KEYSPAN) || \
  150.     defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE) || \
  151.     defined(CONFIG_USB_SERIAL_WHITEHEAT_MODULE) || \
  152.     defined(CONFIG_USB_SERIAL_KEYSPAN_MODULE)
  153.   #define USES_EZUSB_FUNCTIONS
  154.   extern int  ezusb_writememory(struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest);
  155. extern int  ezusb_set_reset(struct usb_serial *serial, unsigned char reset_bit);
  156. #else
  157. #undef USES_EZUSB_FUNCTIONS
  158. #endif
  159.  
  160. /* Inline functions to check the sanity of a pointer that is passed to us */
  161. static inline int serial_paranoia_check(struct usb_serial *serial, const char *function)
  162. {
  163.   if(!serial) {
  164.     dbg("%s - serial == NULL", function);
  165.     return -1;
  166.   }
  167.  
  168.   if(serial->magic != USB_SERIAL_MAGIC) {
  169.     dbg("%s - bad magic number for serial", function);
  170.     return -1;
  171.   }
  172.  
  173.   if(!serial->type) {
  174.     dbg("%s - serial->type == NULL!", function);
  175.     return -1;
  176.   }
  177.  
  178.   return 0;
  179. }
  180.  
  181. static inline int port_paranoia_check(struct usb_serial_port *port, const char *function)
  182. {
  183.   if(!port) {
  184.     dbg("%s - port == NULL", function);
  185.     return -1;
  186.   }
  187.  
  188.   if(port->magic != USB_SERIAL_PORT_MAGIC) {
  189.     dbg("%s - bad magic number for port", function);
  190.     return -1;
  191.   }
  192.  
  193.   if(!port->serial) {
  194.     dbg("%s - port->serial == NULL", function);
  195.     return -1;
  196.   }
  197.  
  198.   if(!port->tty) {
  199.     dbg("%s - port->tty == NULL", function);
  200.     return -1;
  201.   }
  202.  
  203.   return 0;
  204. }
  205.  
  206. static inline struct usb_serial *get_usb_serial(struct usb_serial_port *port, const char *function)
  207. {
  208.   /* if no port was specified, or it fails a paranoia check */
  209.   if(!port || port_paranoia_check(port, function) || serial_paranoia_check(port->serial, function)) {
  210.     /* then say that we dont have a valid usb_serial thing, which will
  211.          * end up genrating -ENODEV return values */
  212.     return NULL;
  213.   }
  214.  
  215.   return port->serial;
  216. }
  217.  
  218. static inline void usb_serial_debug_data(const char *file, const char *function, int size, const unsigned char *data)
  219. {
  220. #ifdef USB_SERIAL_DEBUG
  221.  
  222.   int i;
  223.   printk(KERN_DEBUG "%s: %s - length = %d, data = ", file, function, size);
  224.   for(i = 0; i < size; ++i) {
  225.     printk("%.2x ", data[i]);
  226.   }
  227.  
  228.   printk("\n");
  229. #endif
  230. }
  231. #endif /* ifdef __LINUX_USB_SERIAL_H */
  232.